//
//  AccountViewController.m
//  ShangAPP
//
//  Created by 徐番茄丶 on 16/1/22.
//  Copyright © 2016年 wangdao. All rights reserved.
//

#import "AccountViewController.h"
#import "MyTools.h"
@interface AccountViewController (){
    
}
@property (weak, nonatomic) IBOutlet UITextField *weifield;
@property (weak, nonatomic) IBOutlet UITextField *alifield;
@property (weak, nonatomic) IBOutlet UITextField *namefield;
@property (weak, nonatomic) IBOutlet UIButton *mybtn;

@end

@implementation AccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"账户绑定";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0x444444)];
    
    self.view.backgroundColor = UIColorFromRGB(0xefeff1);
    [self getDate];
    //[self viewCreate];
}

-(void)getDate{
    NSString *url = @"http://www.xsy777.com/app/merchant/bankcardinfo.html";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic1 = @{@"user_token":[user objectForKey:@"token"]};
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:url parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:[responseObject objectForKey:@"data"] ];
            _alistr = [NSString stringWithFormat:@"%@",[dic objectForKey:@"alipay"]];
            _weistr = [NSString stringWithFormat:@"%@",[dic objectForKey:@"accounts"]];
            _namestr = [NSString stringWithFormat:@"%@",[dic objectForKey:@"name"]];
            //NSLog(@"%@",dic);
            [self viewCreate];
            //[_myTab reloadData];
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
    
}

-(void)viewCreate{
    if ([_alistr intValue] != 0 || [_weistr intValue] != 0) {
        [_mybtn setTitle:@"修改绑定" forState:UIControlStateNormal];
        
    }else{
        [_mybtn setTitle:@"立即绑定" forState:UIControlStateNormal];
    }
    
    if ([_alistr intValue] == 0) {
        _alifield.text = _alistr;
    }
    
    if ([_weistr intValue] == 0) {
        _weifield.text = _weistr;
    }
    if ([_namestr intValue] == 0) {
        _namefield.text = _namestr;
    }
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    btn.backgroundColor = [UIColor clearColor];
    [btn addTarget:self action:@selector(fieldBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:btn atIndex:0];
}


- (IBAction)changemessage:(id)sender {
    if (_alifield.text.length == 0 && _weifield.text.length == 0){
        [self.view makeToast:@"请选择账户填写" duration:0.5 position:CSToastPositionCenter];
    }else if (_namefield.text.length == 0) {
        [self.view makeToast:@"请填写姓名" duration:0.5 position:CSToastPositionCenter];
    }else{
        [self getcard];
    }
    
}

-(void)getcard{
    NSString *url = @"http://www.xsy777.com/app/merchant/bankcardedit.html";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = @{@"user_token":[user objectForKey:@"token"],
                          @"accounts":_weifield.text,
                          @"alipay":_alifield.text,
                          @"name":_namefield.text};
//    NSLog(@"%@",dic);
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    
    [manager POST:url parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            [self.view makeToast:@"绑定成功" duration:0.5 position:CSToastPositionCenter];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:0.5 position:CSToastPositionCenter];
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}





-(void)fieldBack{
    [_namefield resignFirstResponder];
    [_alifield resignFirstResponder];
    [_weifield resignFirstResponder];
}



-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
